package com.gorkr.labuladong.data_struct.stack;

import java.util.HashMap;
import java.util.Stack;

/**
 * @author gorkr
 * @date 2022/06/01 22:01
 **/
public class E496NextGreaterElementI {
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int[] list = greaterList(nums2);
        for (int i = 0; i < nums2.length; i++) {
            map.put(nums2[i],list[i]);
        }
        int[] res = new int[nums1.length];
        for (int i = 0; i < nums1.length; i++) {
            res[i] = map.get(nums1[i]);
        }


        return res;
    }

    private int[] greaterList(int[] nums){
        int length = nums.length;
        int[] greaterList = new int[length];

        Stack<Integer> stack = new Stack<>();
        for (int i = length-1; i >=0 ; i--) {
            while (!stack.isEmpty() && stack.peek()<nums[i]){
                stack.pop();
            }
            // 最重要
            greaterList[i] = stack.isEmpty()? -1:stack.peek();
            stack.push(nums[i]);
        }
        return greaterList;

    }




}
    